package qeb.handheldmedical.exam;

import qeb.handheldmedical.R;
import android.app.Activity;
import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.TextView;

/**
 * @author 作者(dugaoyi) E-mail:dugaoyi@163.com
 * @version 创建时间：2016年10月15日 下午1:29:02
 * @declaration
 */
public class OpenExamFile extends Activity {

	private WebView webView1 = null;
	private TextView tvwebtitle = null;
	Button btn_title_back, btn_title_right;
	private TextView tv_top_title;
	private String examTitle;
	private String fileName;

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		fileName = (String) getIntent().getSerializableExtra("fileName");
		examTitle = (String) getIntent().getSerializableExtra("examTitle");
		setContentView(R.layout.open_exam_file);
		webView1 = (WebView) findViewById(R.id.webView1);
		tvwebtitle = (TextView) findViewById(R.id.tvwebtitle);
		// 支持javascript
		webView1.getSettings().setJavaScriptEnabled(true);
		webView1.getSettings().setDefaultTextEncodingName("gb2312");
		// 此方法可以处理webview 在加载时和加载完成时一些操作

		tv_top_title = (TextView) findViewById(R.id.tv_top_title);
		tv_top_title.setText(examTitle);
		btn_title_back = (Button) findViewById(R.id.btn_title_back);
		btn_title_back.setOnClickListener(MyOnClickListener);
		btn_title_right = (Button) findViewById(R.id.btn_title_right);
		btn_title_right.setOnClickListener(MyOnClickListener);
		webView1.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {

				if (newProgress == 100) {

					// 这里是设置activity的标题， 也可以根据自己的需求做一些其他的操作

					tvwebtitle.setText("加载完成");
					tvwebtitle.setVisibility(View.GONE);

				} else {
					tvwebtitle.setVisibility(View.VISIBLE);
					tvwebtitle.setText("加载中…….");
				}

			}

		});

		webView1.setWebViewClient(new WebViewClient() {
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				// TODO Auto-generated method stub
				view.loadUrl(url);
				return true;
			}

			@Override
			public void onReceivedSslError(WebView view,
					SslErrorHandler handler, SslError error) {
				// TODO Auto-generated method stub
				handler.proceed();
			}

		});
		String url = "http://api.idocv.com/view/url?url=http://61.153.43.244:8989/StudyFile/"
				+ fileName + "&name=";
		webView1.loadUrl(url);

	}

	private OnClickListener MyOnClickListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub

			switch (v.getId()) {
			// 退回
			case R.id.btn_title_back:
				OpenExamFile.this.finish();
				break;
			default:
				break;
			}
		}
	};
}
